---
layout: docs
page_title: Storing the Gossip Encryption Key in Vault
description: >-
  Configuring the Consul Helm chart to use a gossip encryption key stored in Vault.
---

# Storing Gossip Encryption Key in Vault

## Overview
To use a gossip encryption key stored in Vault, we will follow the steps outlined in the [Data Integration](/docs/k8s/installation/vault/data-integration) section:

### One time setup in Vault
  1. Store the secret in Vault.
  1. Create a Vault policy that authorizes the desired level of access to the secret.
  
### Setup per Consul datacenter
  1. Create Vault Kubernetes auth roles that link the policy to each Consul on Kubernetes service account that requires access.
  1. Update the Consul on Kubernetes helm chart.

## Prerequisites
Prior to setting up the data integration between Vault and Consul on Kubernetes, you will need to have:  
1. Read and completed the steps in the [Systems Integration](/docs/k8s/installation/vault/systems-integration) section of [Vault as a Secrets Backend](/docs/k8s/installation/vault).
2. Read the [Data Integration Overview](/docs/k8s/installation/vault/data-integration) section of [Vault as a Secrets Backend](/docs/k8s/installation/vault).

## One time setup in Vault
### Store the Secret in Vault
First, generate and store the gossip key in Vault:

```shell-session
$ vault kv put secret/consul/gossip key="$(consul keygen)"
```
### Create a Vault policy that authorizes the desired level of access to the secret

-> **Note:** The secret path referenced by the Vault Policy below will be your `global.gossipEncryption.secretName` Helm value.

Next, we will need to create a policy that allows read access to this secret:

<CodeBlockConfig filename="gossip-policy.hcl">

```HCL
path "secret/data/consul/gossip" {
  capabilities = ["read"]
}
```

</CodeBlockConfig>

Apply the Vault policy by issuing the `vault policy write` CLI command:

```shell-session
$ vault policy write gossip-policy gossip-policy.hcl
```

## Setup per Consul datacenter
### Create Vault Kubernetes auth roles that link the policy to each Consul on Kubernetes service account that requires access

Next, we will create Kubernetes auth roles for the Consul server and client:

```shell-session
$ vault write auth/kubernetes/role/consul-server \
    bound_service_account_names=<Consul server service account> \
    bound_service_account_namespaces=<Consul installation namespace> \
    policies=gossip-policy \
    ttl=1h
```

```shell-session
$ vault write auth/kubernetes/role/consul-client \
    bound_service_account_names=<Consul client service account> \
    bound_service_account_namespaces=<Consul installation namespace> \
    policies=gossip-policy \
    ttl=1h
```

To find out the service account names of the Consul server and client,
you can run the following `helm template` commands with your Consul on Kubernetes values file:

- Generate Consul server service account name
  ```shell-session
  $ helm template --release-name ${RELEASE_NAME} -s templates/server-serviceaccount.yaml hashicorp/consul
  ```

- Generate Consul client service account name
  ```shell-session
  $ helm template --release-name ${RELEASE_NAME} -s templates/client-serviceaccount.yaml hashicorp/consul
  ```

### Update the Consul on Kubernetes helm chart

Now that we've configured Vault, you can configure the Consul Helm chart to
use the gossip key in Vault:

<CodeBlockConfig filename="values.yaml">

```yaml
global:
  secretsBackend:
    vault:
      enabled: true
      consulServerRole: consul-server
      consulClientRole: consul-client
  gossipEncryption:
    secretName: secret/data/consul/gossip
    secretKey: key
```

</CodeBlockConfig>

Note that `global.gossipEncryption.secretName` is the path of the secret in Vault.
This should be the same path as the one you'd include in your Vault policy.
`global.gossipEncryption.secretKey` is the key inside the secret data. This should be the same
as the key we passed when we created the gossip secret in Vault.
